import { req } from '@/util/request'
import type { Page, PageOprion, ResData } from '@/types/index'
import type { DictKeyVal, DictTable } from '@/types/dict'
import { deepCopy } from '@/util/token'

export function searchDict(path: string, type: string): ResData<DictKeyVal[]> {
  return req.get('/dict/search/kv', {
    params: {
      type,
      path
    }
  })
}

export function getDictTableData(page: PageOprion, cdn: DictTable = {}): ResData<Page<DictTable>> {
  return req.post('/dict/get/all', {
    num: page.num,
    size: page.size,
    cdn
  })
}

export function delDict(ids: number[]): ResData<any> {
  return req.post('/dict/delete', {
    ids
  })
}

export function addDict(dict: DictTable): ResData<any> {
  return req.post('/dict/add', dict)
}

export function updateDict(dict: DictTable): ResData<any> {
  return req.post('/dict/update', dict)
}

export function getDictKeyVal(did: number): ResData<DictKeyVal[]> {
  return req.get('/dict/get_key_val_by_id', {
    params: {
      dId: did
    }
  })
}

export function updateDictKeyVal(dId: number, data: DictKeyVal[]): ResData<any> {
  data = deepCopy(data)
  return req.post('/dict/update/all_key_val', {
    dId,
    data
  })
}
